package pers.zc.activiti.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @description
 * @Author Lin FuYuan
 * @date 2020/7/2
 */
public class NettyServer {
    public static void main(String[] args) {
        //nio循环事物组  指定线程数为1
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        //nio循环事务组  工作组 使用默认线程数 0
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //服务器
            ServerBootstrap server = new ServerBootstrap();
            server.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(8080)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new EchoHandler());
                        }
                    });
            //启动
            ChannelFuture future = server.bind().sync();
            System.out.println("启动服务端");
            //关闭钩子,关闭时调用
            future.channel().closeFuture().sync();

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            System.out.println("关闭服务端");
        }


    }
}
